const app = getApp()
Page({
  data: {
      totalHeight: 0,
      headList: [
          { title: '产品名称' },
          { title: '销售数量' },
          { title: '销售单价' },
          { title: '税率' },
          { title: '销售额' },
          { title: '折扣' },
          { title: '实销额' },
          { title: '净收入'  },
          { title: '成本'  },
          { title: '运费额'  },
          { title: '运费率'  },
          { title: '毛利额'  },
          { title: '毛利率'  },
          { title: '专项费用'  },
          { title: '专项费率'  },
          { title: '其他费用'  },
          { title: '行政费用'  },
          { title: '利润'  },
          { title: '利润率'  },
      ],
      footerList:['汇总',20,'','','','','','','','','','','','','','','','',''],
      trList: []
  },
  onInputChange(e){
    console.log(e)
    const index = e.currentTarget.dataset.index
    const field = e.currentTarget.dataset.field
    const value = e.detail.value
    console.log(index,field,value)
    if(field){
      const orgTrList = this.data.trList
      orgTrList[index][field] = value
      this.setData({
        trList: orgTrList
      })
    }
    this.reCompute()
  },

  reCompute() {
    const orgList = this.data.trList
    orgList.forEach(row=>{
      const quantity = row.quantity
      const taxRate = row.taxRate
      const factoryPrice = row.factoryPrice
      const factoryFee = quantity * factoryPrice
      const costFee = row.costFee
      const excludingTaxCosts = row.excludingTaxCosts
      const yunfeil = row.yunfeil
      const saleFee = factoryFee - costFee
      const jingshouru = saleFee/taxRate
      const excludingTaxCostsFee = excludingTaxCosts* quantity
      const yunfeie = factoryFee*yunfeil*0.01
      const maolie = jingshouru - excludingTaxCostsFee - yunfeie
      const maolil = saleFee === 0?0: maolie/saleFee/0.01
      const zhuanxiangfeil = row.zhuanxiangfeil
      const zhuanxiangfeiy = zhuanxiangfeil * factoryFee * 0.01
      const xingzhengfeiyong = row.xingzhengfeiyong
      const qitafeiyong = row.qitafeiyong
      const lirun = maolie - qitafeiyong - xingzhengfeiyong - zhuanxiangfeiy
      const lirunl = lirun / factoryFee / 0.01
      row.factoryFee = factoryFee
      row.costFee = costFee
      row.saleFee = saleFee
      row.jingshouru = jingshouru
      row.excludingTaxCostsFee = excludingTaxCostsFee
      row.yunfeie = yunfeie
      row.maolie = maolie
      row.maolil = maolil
      row.zhuanxiangfeil = zhuanxiangfeil
      row.zhuanxiangfeiy = zhuanxiangfeiy
      row.xingzhengfeiyong = xingzhengfeiyong
      row.qitafeiyong = qitafeiyong
      row.lirun = lirun
      row.lirunl = lirunl
    })
    const footerList = this.data.footerList
    footerList[1] = orgList.map(row=>row.quantity).reduce((prev, next) => Number(prev) + Number(next))
    footerList[2] = orgList.map(row=>row.factoryPrice).reduce((prev, next) => Number(prev) + Number(next))
    footerList[4] = orgList.map(row=>row.factoryFee).reduce((prev, next) => Number(prev) + Number(next))
    footerList[5] = orgList.map(row=>row.costFee).reduce((prev, next) => Number(prev) + Number(next))
    footerList[6] = orgList.map(row=>row.saleFee).reduce((prev, next) => Number(prev) + Number(next))
    footerList[7] = orgList.map(row=>row.jingshouru).reduce((prev, next) => Number(prev) + Number(next))
    footerList[8] = orgList.map(row=>row.excludingTaxCostsFee).reduce((prev, next) => Number(prev) + Number(next))
    footerList[9] = orgList.map(row=>row.yunfeie).reduce((prev, next) => Number(prev) + Number(next))
    footerList[10] = footerList[9]/footerList[4] *100
    footerList[11] = orgList.map(row=>row.maolie).reduce((prev, next) => Number(prev) + Number(next))
    footerList[12] = footerList[11] / footerList[6] *100
    footerList[13] = orgList.map(row=>row.zhuanxiangfeiy).reduce((prev, next) => Number(prev) + Number(next))
    footerList[14] = footerList[13] / footerList[4] *100
    footerList[15] = orgList.map(row=>row.qitafeiyong).reduce((prev, next) => Number(prev) + Number(next))
    footerList[16] = orgList.map(row=>row.xingzhengfeiyong).reduce((prev, next) => Number(prev) + Number(next))
    footerList[17] = orgList.map(row=>row.lirun).reduce((prev, next) => Number(prev) + Number(next))
    footerList[18] =  footerList[17]/footerList[4] *100
    this.setData({
      trList: orgList,
      footerList: footerList
    })
  },

  // 监听页面加载
  onLoad() {
      wx.getSystemInfo({
          success: (res) => {
              this.setData({
                  totalHeight: res.windowHeight - 140
              })
          }
      })
  },
  onShow(){
    app.func.request({
      url: '/finance/budget/batch/list',
      method: 'get',
      success:({data: cartIdList})=>{
        app.func.request({
          url:'/finance/budget/page',
          data:{
            baseParams:{
              idList: [...cartIdList,'0']
            }
          },
          success:({data: skuList})=>{

            skuList = skuList.map(row=>{
              return {...row,
                quantity: 1,
                factoryFee: row.factoryPrice,
                costFee: 0,
                saleFee: row.factoryPrice,
                yunfeil: 5,
                zhuanxiangfeil: 15,
                qitafeiyong: 0,
                xingzhengfeiyong: 0,
              }
            })
            skuList = skuList.map(row=>{
              row.factoryFee = (row.quantity*row.factoryPrice);
              row.costFee = (row.factoryFee-  row.saleFee);
              row.jingshouru = (row.saleFee/ row.taxRate);
              row.excludingTaxCostsFee = (row.excludingTaxCosts * row.quantity);
              row.yunfeie = (row.factoryFee* row.yunfeil* 0.01);
              row.maolie = (row.jingshouru- row.excludingTaxCostsFee- row.yunfeie);
              row.maolil = row.saleFee === 0 ? 0 : (row.maolie/row.saleFee/ 0.01);
              row.zhuanxiangfeiy = (row.factoryFee * row.zhuanxiangfeil* 0.01);
              row.lirun = (row.maolie- row.qitafeiyong- row.xingzhengfeiyong- row.zhuanxiangfeiy);
              row.lirunl = (row.lirun/ row.factoryFee/ 0.01);
              return row;
            })
              this.setData({
                trList: skuList
              })
              this.reCompute()
               
          }
        })
      }
    })
  }
})
 